//
//  ReleaseStep2ViewController.m
//  Gunz
//
//  Created by Joggy on 16/4/15.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "ReleaseStep2ViewController.h"
#import "BarButtonView.h"
#import "ReleaseStep3ViewController.h"
#import "QYNotice.h"

@interface ReleaseStep2ViewController ()

@end

@implementation ReleaseStep2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    NSLog(@"%@", _data);
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"任务发布-步骤2";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [GZConst getScreenWidth], [GZConst getScreenHeight] - 64 - 56)];
    scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height + 1);
    [self.view addSubview:scroll];
    //底部按钮
    BarButtonView *barButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, [GZConst getScreenHeight] - 64 - BBViewHeight, [GZConst getScreenWidth], BBViewHeight) name:@"下一步"];
    [barButton.but addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:barButton];
    //标题lab
    UILabel *titleConfig = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 76, 17)];
    titleConfig.layer.position = CGPointMake([GZConst getScreenWidth]/2, 34);
    titleConfig.textColor = CUSTOM_GREEN;
    titleConfig.font = [UIFont systemFontOfSize:14];
    titleConfig.textAlignment = NSTextAlignmentCenter;
    titleConfig.text = @"任务需求";
    [scroll addSubview:titleConfig];
    taskStyle = [[Step2Select alloc] initWithTitle:@"任务类型:" pointOne:@"单人任务" pointTwo:@"群体任务"];
    taskStyle.layer.position = CGPointMake([GZConst getScreenWidth]/2, 93);
    [taskStyle.pointOne addTarget:self action:@selector(taskSingle) forControlEvents:UIControlEventTouchUpInside];
    [taskStyle.pointTwo addTarget:self action:@selector(taskGroup) forControlEvents:UIControlEventTouchUpInside];
    [scroll addSubview:taskStyle];
}

#pragma 事件方法

//- (void)

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)taskSingle {
    taskStyle.pointOne.selected = !taskStyle.pointOne.selected;
    if(taskStyle.pointOne.selected) {
        taskStyle.pointTwo.selected = false;
        if(taskGroup != nil) {
            [taskGroup removeFromSuperview];
        }
        if(taskGroupGoal != nil) {
            [taskGroupGoal removeFromSuperview];
        }
        if(taskGroupLabel != nil) {
            [taskGroupLabel removeFromSuperview];
        }
        [self adjustScrollContent:taskStyle.frame.origin.y + taskStyle.frame.size.height];
    }
}

- (void)taskGroup {
    taskStyle.pointTwo.selected = !taskStyle.pointTwo.selected;
    if(taskStyle.pointTwo.selected) {
        taskStyle.pointOne.selected = false;
        taskGroup = [[Step2Select alloc] initWithTitle:@"群体任务要求:" pointOne:@"全部完成" pointTwo:@"可部分完成"];
        taskGroup.layer.position = CGPointMake([GZConst getScreenWidth]/2, 167);
        [taskGroup.pointOne addTarget:self action:@selector(taskCompleteAll) forControlEvents:UIControlEventTouchUpInside];
        [taskGroup.pointTwo addTarget:self action:@selector(taskCompletePart) forControlEvents:UIControlEventTouchUpInside];
        [scroll addSubview:taskGroup];
        [self adjustScrollContent:taskGroup.frame.origin.y + taskGroup.frame.size.height];
    }
    else {
        if(taskGroup != nil) {
            [taskGroup removeFromSuperview];
        }
        if(taskGroupGoal != nil) {
            [taskGroupGoal removeFromSuperview];
        }
        if(taskGroupLabel != nil) {
            [taskGroupLabel removeFromSuperview];
        }
        [self adjustScrollContent:taskStyle.frame.origin.y + taskStyle.frame.size.height];
    }
}

- (void)taskCompleteAll {
    taskGroup.pointOne.selected = !taskGroup.pointOne.selected;
    if(taskGroup.pointOne.selected) {
        taskGroup.pointTwo.selected = false;
        if(taskGroupGoal != nil) {
            [taskGroupGoal removeFromSuperview];
        }
        taskGroupLabel = [[Step2Label alloc] initWithTitle:@"群体任务目标:" lab:@"社会效益最大化"];
        taskGroupLabel.layer.position = CGPointMake([GZConst getScreenWidth]/2, 241);
        [scroll addSubview:taskGroupLabel];
        [self adjustScrollContent:taskGroupLabel.frame.origin.y + taskGroupLabel.frame.size.height];
    }
    else {
        if(taskGroupLabel != nil) {
            [taskGroupLabel removeFromSuperview];
        }
        [self adjustScrollContent:taskGroup.frame.origin.y + taskGroup.frame.size.height];
    }
}

- (void)taskCompletePart {
    taskGroup.pointTwo.selected = !taskGroup.pointTwo.selected;
    if(taskGroup.pointTwo.selected) {
        taskGroup.pointOne.selected = false;
        if(taskGroupLabel != nil) {
            [taskGroupLabel removeFromSuperview];
        }
        taskGroupGoal = [[Step2Select alloc] initWithTitle:@"群体任务目标:" pointOne:@"发布者效益最大化" pointTwo:@"发布效益最大化"];
        taskGroupGoal.layer.position = CGPointMake([GZConst getScreenWidth]/2, 241);
        [taskGroupGoal.pointOne addTarget:self action:@selector(taskLauncherMax) forControlEvents:UIControlEventTouchUpInside];
        [taskGroupGoal.pointTwo addTarget:self action:@selector(taskReleaseMax) forControlEvents:UIControlEventTouchUpInside];
        [scroll addSubview:taskGroupGoal];
        [self adjustScrollContent:taskGroupGoal.frame.origin.y + taskGroupGoal.frame.size.height];
    }
    else {
        if(taskGroupGoal != nil) {
            [taskGroupGoal removeFromSuperview];
        }
        [self adjustScrollContent:taskGroup.frame.origin.y + taskGroup.frame.size.height];
    }
}

- (void)taskLauncherMax {
    taskGroupGoal.pointOne.selected = !taskGroupGoal.pointOne.selected;
    if(taskGroupGoal.pointOne.selected) {
        taskGroupGoal.pointTwo.selected = false;
    }
}

- (void)taskReleaseMax {
    taskGroupGoal.pointTwo.selected = !taskGroupGoal.pointTwo.selected;
    if(taskGroupGoal.pointTwo.selected) {
        taskGroupGoal.pointOne.selected = false;
    }
}

- (void)adjustScrollContent:(CGFloat)bottom {
    if(scroll.frame.size.height < bottom) {
        scroll.contentSize = CGSizeMake(scroll.frame.size.width, bottom);
    }
    else {
        scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height + 1);
    }
}

- (NSString*)collectData {
    if(!taskStyle.pointOne.selected && !taskStyle.pointTwo.selected) {
        return @"请选择任务类型";
    }
    else {
        if(taskStyle.pointOne.selected) {
            _data[@"typetask"] = @"1";
            return @"noerror";
        }
        else {
            _data[@"typetask"] = @"2";
            if(!taskGroup.pointOne.selected && !taskGroup.pointTwo.selected) {
                return @"请选择群体任务要求";
            }
            else {
                if(taskGroup.pointOne.selected) {
                    _data[@"requiretask"] = @"1";
                    return @"noerror";
                }
                else {
                    _data[@"requiretask"] = @"2";
                    if(!taskGroupGoal.pointOne.selected && !taskGroupGoal.pointTwo.selected) {
                        return @"请选择群体任务目标";
                    }
                    else {
                        if(taskGroupGoal.pointOne.selected) {
                            _data[@"aimtask"] = @"2";
                        }
                        else {
                            _data[@"aimtask"] = @"1";
                        }
                        return @"noerror";
                    }
                }
            }
        }
    }
}

- (void)nextStep {
    NSString *errorInfo = [self collectData];
    if([errorInfo isEqualToString:@"noerror"]) {
        ReleaseStep3ViewController *next = [[ReleaseStep3ViewController alloc] init];
        next.data = _data;
        [self.navigationController pushViewController:next animated:true];
    }
    else {
        [QYNotice showError:errorInfo toView:self.view];
        [QYNotice dismissWithDelay:3];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
